const jwt = require('jsonwebtoken');
const { PRIVATE_KEY } = require('../app/config');
const { API_message_200, API_message_500 } = require('../app/constants/messageAPI');

class AuthController {
    async login(ctx) {
        const { id, username } = ctx.user;
        // console.log(ctx.user)

        const token = jwt.sign({id, username}, PRIVATE_KEY, {
            expiresIn: 60* 60 * 24,
            algorithm: 'RS256'
        })
        try {
            let param = { id, username, token }
            ctx.body = API_message_200({ ...param }, "登录成功")
        } catch(e) {
            // console.log("token获取失败", e);
            ctx.body = API_message_500('后台未知错误');
        }
        
    }

    async success(ctx) {
        ctx.body = "授权成功";
    }
}

module.exports = new AuthController();